
export default async function ({ store, route, $post, redirect, req }) {
  const isRouter = !!route.name
  if (!isRouter) return

  if (route.path === '/err') return

  const IsPc = isPc(getUserAgent(req))

  if (IsPc) return

  const backPath = encodeURIComponent(route.fullPath || '/')

  if (!store.state.accessToken) {
    console.log('no_accessToken')
    redirect(`/api/auth?backPath=${backPath}`)
  } else {
    let authUser = store.state.authUser

    if (authUser) return
    const resp = await $post('/api/platform/user/findUserByToken', {})

    authUser = resp && resp.data
    if (authUser && authUser.user) {
      console.log('has_authUser')
      store.commit('SET_USER', authUser)
      return
    }
    console.log('no_authUser')
    redirect(`/api/auth?backPath=${backPath}`)
  }
}

function getUserAgent(req) {
  const isClient = process.client
  const userAgent = isClient ? navigator.userAgent : req.headers['user-agent']
  return userAgent
}
function isPc(userAgent) {
  if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(userAgent)) {
    return false
  } else {
    return true
  }
}
